package Demo1;

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: homechen
 * Date: 2024-03-11
 * Time: 20:31
 */
public class ThreadDemo4 {
    public static void main(String[] args) {
        ThreadGroup threadGroup = new ThreadGroup("MyThreadGroup"); // 线程组
        // 线程工厂
        ThreadFactory threadFactory = new ThreadFactory() {
            @Override
            public Thread newThread(Runnable r) {
                // 创建线程池中的线程（组名 + 执行的任务）
                Thread thread = new Thread(threadGroup, r);
                thread.setName("Thread-" + r.hashCode()); // 线程名
                thread.setPriority(Thread.MAX_PRIORITY); // 10 但也只是建议
                // ...
                return thread;
            }
        };

        // 创建线程池
        ThreadPoolExecutor executor = new ThreadPoolExecutor(
                10, 10, 1,
                TimeUnit.SECONDS, new LinkedBlockingQueue<>(),
                threadFactory);
        // 使用线程池
        executor.submit(new Runnable() {
            @Override
            public void run() {
                // 当前线程
                Thread thread = Thread.currentThread();
                System.out.println(String.format("线程：%s | 线程的优先级：%d",
                        thread.getName(), thread.getPriority()));
            }
        });

//        ThreadPoolExecutor executor1 = new ThreadPoolExecutor(10,10,
//                1,TimeUnit.SECONDS,
//                new LinkedBlockingQueue<>(),
//                threadFactory,new ThreadPoolExecutor.)
    }
}
